package cn.aitstone.system.service.impl;

import cn.aitstone.system.domain.Role;
import cn.aitstone.system.mapper.RoleMapper;
import cn.aitstone.system.service.IRoleService;
import cn.aitstone.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sheldon
 * @since 2022-07-11
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        roleMapper.save(role);
        System.out.println(role);
        roleMapper.saveRoleMenu(role.getId(), role.getMenus());
        roleMapper.saveRolePermission(role.getId(), role.getPermissions());
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
        initRelationTable(role);

    }


    public void initRelationTable(Role role){
        // 1.根据roleId删除角色菜单和角色权限关联表对应数据
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.removeRolePermission(role.getId());
        // 2.根据roleId和菜单Id和权限Id进行新增
        roleMapper.saveRoleMenu(role.getId(), role.getMenus());
        roleMapper.saveRolePermission(role.getId(), role.getPermissions());
    }

    @Override
    public void remove(Long id) {
        roleMapper.removeRolePermission(id);
        roleMapper.remove(id);
        roleMapper.removeRoleMenu(id);


    }

    @Override
    public List<Role> loadByEmpId(Long id) {
        return roleMapper.loadByEmpId(id);
    }
}
